
const mRequest = require('../../../utils/api.js')
Page({
  data: {
    tempFilePaths: [],
    //以下为上传图片至云存储
    images_fileID: [],
  },

  //选择图片
  ChooseImg: function () {
    let that = this;
    wx.chooseImage({
      count: 1, // 默认最多9张图片，可自行更改
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: res => {
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 1000
        })

        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFilePath = res.tempFilePaths;
    
        that.setData({
          tempFilePaths: tempFilePath
        })
      }
    })
  },

  //预览图片
  PreviewImg: function (e) {
    let index = e.target.dataset.index;
    let that = this;
    //console.log(that.data.tempFilePaths[index]);
    //console.log(that.data.tempFilePaths);
    wx.previewImage({
      current: that.data.tempFilePaths[index],
      urls: that.data.tempFilePaths,
    })
  },

  //长按删除图片
  DeleteImg: function (e) {
    var that = this;
    var tempFilePaths = that.data.tempFilePaths;
    var index = e.currentTarget.dataset.index;//获取当前长按图片下标
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          //console.log('点击确定了');
          tempFilePaths.splice(index, 1);
        } else if (res.cancel) {
          //console.log('点击取消了');
          return false;
        }
        that.setData({
          tempFilePaths
        });
      }
    })
  },

  // 上传图片至云数据库,可自行参考

  UploadBtntap: function (e) {
    console.log(e)
    var count = 0;
    var h = this.data.tempFilePaths.length;
    if (h != 0) {
      this.setData({
        images_fileID: []
      })
    }
    for (var i = 0; i < h; i++) {
      //上传文件
      var imageUrl = this.data.tempFilePaths[i].split("/");
      var name = imageUrl[imageUrl.length - 1];
      wx.getFileSystemManager().readFile({
        filePath: this.data.tempFilePaths[i],
        encoding: 'base64',
        position: 0,
        success: res => {
          var content = res.data
          var params = {
            title: new Date().toLocaleString('zh').replaceAll('/', '-').replaceAll('下午','_',).replaceAll(':','-').replaceAll('上午','_') + '.jpg',
            // title: name,
            data: {
            "access_token": wx.getStorageSync('access_token'),
            "content": content,
            "message": "上传成功",
            "branch": "gh-pages"
            }
          }
          mRequest.insertImg(params).then(res => {
              wx.showToast({
                title: '提交成功' + res.commit.message,
                icon: 'none',
                duration: 2000
              });
              console.log('res_content: ', res.content);
          }).catch(err => {
            console.log('error: ', err);
          })
        },
        fail(res) {
          console.error(res)
          count++;
          wx.hideToast();
          wx.showModal({
            title: '错误提示',
            content: '上传图片失败',
            showCancel: false,
            success: function (res) { }
          })
        }
      })
    }
  }
})

  // 上传图片至云数据库,可自行参考
/*
  UploadBtntap: function (e) {
    var count = 0;
    var h = this.data.tempFilePaths.length;
    if (h != 0) {
      this.setData({
        images_fileID: []
      })
    }
    for (var i = 0; i < h; i++) {
      //上传文件
      var imageUrl = this.data.tempFilePaths[i].split("/");
      var name = imageUrl[imageUrl.length - 1];
      var images_fileID = this.data.images_fileID;
      console.log(images_fileID)
      wx.cloud.uploadFile({
        cloudPath: name,    //自定义云存储路径
        filePath: this.data.tempFilePaths[i],
        success: res => {
          images_fileID.push(res.fileID);
          this.setData({
            images_fileID: images_fileID        //更新data中的 fileID
          })
          fail: res => {
            count++;
            wx.hideToast();
            wx.showModal({
              title: '错误提示',
              content: '上传图片失败',
              showCancel: false,
              success: function (res) { }
            })
          }
        }
      });
    }
  }
})
*/